package com.sanyang.lawyer.controller.audit;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sanyang.lawyer.ResultApi.CodeMsg;
import com.sanyang.lawyer.ResultApi.ResponseResult;
import com.sanyang.lawyer.entity.Audit;
import com.sanyang.lawyer.entity.Case_audit;
import com.sanyang.lawyer.entity.Case_law;
import com.sanyang.lawyer.service.Audit.AuditService;
import com.sanyang.lawyer.service.Audit.Audit_remissionService;
import com.sanyang.lawyer.service.Case.Case_lawService;
import com.sanyang.lawyer.utils.ActivitiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("remission")
public class Audit_remissionController {  //费用审核
    
    @Autowired
    private ActivitiUtils activitiUtils;
    
    @Autowired
    private ObjectMapper objectMapper = new ObjectMapper();
    
    @Autowired
    private Audit_remissionService remissionService;
    
    @Autowired
    private Case_lawService lawService;

    @Autowired
    private AuditService auditService;
    
    
    
    //开始流程
    @RequestMapping("start")
    public ResponseResult<String> start(String data){
        Case_audit audit = null;
        try {
            audit = objectMapper.readValue(data, Case_audit.class);
        } catch (IOException e) {
            //错误返回服务器正忙错误
            ResponseResult.error(CodeMsg.SERVER_BUSY);
        }
        audit.getAuditid().setSubmitTime(new Date());
        
        //开始一个流程
        String processid = activitiUtils.start("委托代理人","my05");
        //给费用表添加数据
        audit.getAuditid().setAuditid(processid);
        remissionService.insertOne(audit);
        
        //给审核表添加数据
        remissionService.insertInAudit(audit);
        //3:将流程推行到下一步操作 进行经理级别的审核
        activitiUtils.Mycomplete(processid);
        return ResponseResult.success("成功添加！~");
    }
    
    //根据流程id查询出对应的案件信息和费用减免金额
    @RequestMapping("queryCaseAndRemissionByProcessid")
    public ResponseResult<Case_audit> queryCaseAndRemissionByProcessid(String processid){
        return ResponseResult.success(remissionService.queryCaseAndRemissionByProcessid(processid));
    }
    
    
    //完成费用减免审核
    @RequestMapping("complateByremission")
    public ResponseResult<String> complateByremission(String data){
        Case_audit case_audit = null;
        try {
            case_audit = objectMapper.readValue(data, Case_audit.class);
        } catch (IOException e) {
            //错误返回服务器正忙错误
            ResponseResult.error(CodeMsg.SERVER_BUSY);
        }
        System.out.println(case_audit);
        //1：添加审核表数据
        remissionService.complateUpdate(case_audit.getAuditid());
        //2：修改案件表数据
        lawService.complateUpdte(case_audit);
        //3：完成流程
        activitiUtils.Mycomplete(case_audit.getAuditid().getAuditid());
        
        //4：将审核状态修改为已审核
        //4：将流程表的审核修改为已审核
        Audit newA = new Audit();
        newA.setState("已审核");
        auditService.update_state(newA);
        
        return ResponseResult.success("完成审核！~");
    }
    
    
    //查询出当前登陆的用户对应.的所有的费用申请
    @RequestMapping("queryremissionAll")
    public ResponseResult<List<Case_audit>> queryremissionAll(String data){
        Case_law case_law = null;
        if (data!=null){
            try {
                case_law = objectMapper.readValue(data, Case_law.class);
            } catch (IOException e) {
                //错误返回服务器正忙错误
                ResponseResult.error(CodeMsg.SERVER_BUSY);
            }
        }
        return ResponseResult.success(remissionService.queryremissionAll(case_law));
    }
    
    
    
}
